﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;

namespace SGArchiveSystem7x.Models
{
    /// <summary>
    /// 菜单模块
    /// </summary>
    public class CMenuModel
    {
        /// <summary>
        /// 菜单
        /// </summary>
        public T_Model Model;
        /// <summary>
        /// 菜单下的子菜单
        /// </summary>
        public List<T_Model> childModel;

        /// <summary>
        /// 建立菜单
        /// </summary>
        /// <returns></returns>
        public static List<CMenuModel> CreateModel()
        {
            List<CMenuModel> list = new List<CMenuModel>();
            var ms = T_ModelDal.Get_Model();
            ///数据出来是排序的，PID为0的排序在最前面，所以一次for循环即可完成
            for(int i=0;i<ms.Count;i++)
            {
                if(ms[i].MD_PID==0)
                {
                    CMenuModel m = new CMenuModel();
                    m.Model = ms[i];
                    m.childModel = new List<T_Model>();
                    list.Add(m);
                }
                else
                {
                    foreach(var s in list)
                    {
                        if(s.Model.MD_ID == ms[i].MD_PID)
                        {
                            s.childModel.Add(ms[i]);
                            break;
                        }
                    }
                }
            }
            return list;
        }
    }
}